package com.qiudaozhang.jblog.article.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.article.mapper.ArticleMapper;
import com.qiudaozhang.jblog.article.mapper.CategoryMapper;
import com.qiudaozhang.jblog.model.Article;
import com.qiudaozhang.jblog.model.Category;
import com.qiudaozhang.jblog.article.service.ArticleService;
import com.qiudaozhang.jblog.article.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author 邱道长
 * 时间： 2018/12/24 11:28
 */
@Service(version = "${service.version}",group = "${dubbo.group}")
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public PageInfo<Category> queryPageParam(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        Category c = new Category();
        c.setName(name);
        List<Category> l = categoryMapper.selectByName(name);
        PageInfo<Category> pageInfo = new PageInfo<>(l);
        return pageInfo;
    }

    @Override
    public void deleteById(Integer categoryId) {
        // 将所有的文章id为categoryId的设置为null
        articleService.updateCategoryNull(categoryId);
        categoryMapper.deleteByPrimaryKey(categoryId);
    }

    @Override
    public List<Category> selectAll() {
        return categoryMapper.selectAll();
    }

    @Override
    public void save(Category category) {
        categoryMapper.insert(category);
    }

    @Override
    public void update(Category category) {
        category.setUpdateTime(new Date());
        categoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public PageInfo<Article> queryArticlePageByCategoryId(Integer pageNum, Integer pageSize, Integer categoryId) {

        PageHelper.startPage(pageNum,pageSize);
        Article a = new Article();
        a.setCategoryId(categoryId);
        List<Article> l = articleMapper.select(a);
        PageInfo<Article> pageInfo = new PageInfo<>(l);
        return pageInfo;
    }
}
